const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        detail: {},
        id: '',
        commentVal: {
            start: 0,
            size: 10,
            articleId: ''
        },
        totalPages: 0,
        commentList: [],
        comment: undefined
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let commentVal = this.data.commentVal;
        commentVal.articleId = options.id;
        this.setData({
            id: options.id,
            commentVal: commentVal
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.getDetail();
        this.getComment();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    // 获取文章详情
    getDetail() {
        let val = {
            articleId: this.data.id
        }
        app.$http.get(app.$api.articleDetail, val).then(({ data, code, msg}) => {
            if(code === 1) {
                wx.setNavigationBarTitle({
                    title: data.theme || '文章详情'
                })
                this.setData({
                    detail: data
                })
            }
        })
    },
    // 获取评论
    getComment() {
        app.$http.get(app.$api.comment, this.data.commentVal).then(({ data, code, msg}) => {
            if(code === 1) {
                let list = this.data.commentList;
                data.data.forEach(item => {
                    list.push(item)
                })
                this.setData({
                    commentList: list,
                    totalPages: data.totalPages - 1
                }, () => {
                    wx.hideLoading();
                })
            }
        })
    },
    // 更新输入框
    updateValue(e) {
        let val = e.detail.value;
        this.setData({
            comment: val
        })
    },
    // 发布评论
    sendOut() {
        if(this.data.comment) {
            let val = {
                comments: this.data.comment,
                aid: this.data.id
            }
            let header = {
                'Content-Type': 'application/json'
            }
            app.$http.post(app.$api.sendOut, val, header).then(({ msg }) => {
                app.$tap.showToast(msg, 'success');
                let commentVal = this.data.commentVal;
                commentVal.start = 0;
                this.setData({
                    comment: '',
                    commentVal: commentVal,
                    commentList: [],
                    totalPages: 0
                }, () => {
                    this.getComment();
                })
            })
        } else {
            app.$tap.showToast('请输入评论内容')
        }
    },
    // 收藏
    collection() {
        let val = {
            articleId: this.data.id
        }
        app.$http.post(app.$api.collection, val).then(({ data }) => {
            if(data) {
                app.$tap.showToast('收藏成功');
            } else {
                app.$tap.showToast('取消成功');
            }
            this.getDetail();
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.totalPages > this.data.commentVal.start) {
            let commentVal = this.data.commentVal;
            commentVal.start += 1;
            this.setData({
                commentVal: commentVal
            }, () => {
                app.$tap.showLoading('加载中', false)
				this.getComment();
            })
        } else {
			app.$tap.showToast('暂无更多数据')
		}
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
		return {
			title: this.data.detail.subtitle || '甘霖',
			imageUrl: this.data.detail.themeImagePath,
			path: `/pages/article/details/details?id=${this.data.detail.id}`
		}
    }
})